package mobi.hami.huapin.service.impl;

import com.github.pagehelper.PageHelper;
import mobi.hami.huapin.dao.CreateNoteAuditMapper;
import mobi.hami.huapin.model.CreateNoteAudit;
import mobi.hami.huapin.model.CreateNoteAuditExample;
import mobi.hami.huapin.model.OpinionLeader;
import mobi.hami.huapin.service.CreateNoteAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Create by 小峰 on 2018/9/13.
 * 新建纸条审核表
 */
@Service
public class CreateNoteAuditServiceImpl implements CreateNoteAuditService {

    @Autowired
    CreateNoteAuditMapper auditMapper;

    /**
     * 用户提交纸条
     *
     * @param reqMap
     * @return
     */
    @Override
    public boolean add(Map<String, String> reqMap) {

        CreateNoteAudit createNoteAudit = new CreateNoteAudit();
        createNoteAudit.setStatus(0);
        createNoteAudit.setUser_id(Integer.parseInt(reqMap.get("userid")));
        createNoteAudit.setComtent(reqMap.get("word"));
        createNoteAudit.setCreate_time(new Date());
        createNoteAudit.setUser_address_id(Long.parseLong(reqMap.get("addressid")));
        auditMapper.insertSelective(createNoteAudit);

        return true;
    }

    /**
     * 获取用户最近5条记录
     *
     * @param reqMap
     * @return
     */
    @Override
    public List<CreateNoteAudit> getTop5List(Map<String, String> reqMap) {

        CreateNoteAuditExample createNoteAuditExample = new CreateNoteAuditExample();
        CreateNoteAuditExample.Criteria criteria = createNoteAuditExample.createCriteria();
        criteria.andUser_idEqualTo(Integer.parseInt(reqMap.get("userid")));
        criteria.andUser_address_idEqualTo(Long.parseLong(reqMap.get("addressid")));

        createNoteAuditExample.setOrderByClause("id desc");

        PageHelper.startPage(1, 5);
        List<CreateNoteAudit> list = auditMapper.selectByExample(createNoteAuditExample);

        return list;
    }

    /**
     * 审核，返回id
     *
     * @param reqMap
     * @return
     */
    @Override
    public int auth(Map<String, String> reqMap) {

        CreateNoteAudit createNoteAudit = new CreateNoteAudit();
        createNoteAudit.setId(Integer.parseInt(reqMap.get("id")));
        createNoteAudit.setStatus(Integer.parseInt(reqMap.get("status")));
        createNoteAudit.setAudit_time(new Date());
        createNoteAudit.setAudit_user_id(Integer.parseInt(reqMap.get("authuserid")));
        createNoteAudit.setAudit_comment(reqMap.get("remark"));

        return auditMapper.updateByPrimaryKeySelective(createNoteAudit);

    }
}
